//
//  SegmentTabbar.m
//  SmallElephant
//
//  Created by Faith on 2024/11/11.
//

#import "PHSegmentTabbar.h"

@interface PHSegmentTabbar()

/// 滚动容器
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *contentContainer;

@property (nonatomic, strong) UIImageView *indictorView;

@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation PHSegmentTabbar

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    [self addSubview:self.scrollView style:@"display:flex; flex: 1; flex-direction: column;"];
}

- (UIScrollView *)scrollView {
    if(!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [_scrollView addSubview:self.contentContainer style:@"display: flex; flex-direction: row;"];
        [_scrollView addSubview:self.indictorView style:@"display: flex; width: 16px; height: 5px; margin-top: 5px;"];
    }
    return _scrollView;
}

- (UIImageView *)indictorView {
    if(!_indictorView) {
        _indictorView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"icon-tab-indictor@3x"]];
    }
    return _indictorView;
}

- (UIView *)contentContainer {
    if(!_contentContainer) {
        _contentContainer = [[UIView alloc] init];
    }
    return _contentContainer;
}

- (void)setData:(NSArray*)dataList {
    self.selectedIndex = 0;
    [self.contentContainer.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for(int i=0;i<dataList.count;i++) {
        UILabel *itemView = [[UILabel alloc] init];
        itemView.tag = i;
        itemView.userInteractionEnabled = YES;
        [itemView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemClick:)]];
        itemView.text = dataList[i];
        [self.contentContainer addSubview:itemView style:@"display: flex; font-size:14px; font-weight: normal; color: #666666;  margin-left: 8px; margin-right: 8px; align-self: center;"];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self itemClick:self.contentContainer.subviews.firstObject.gestureRecognizers.firstObject init:YES];
    });
}

- (void)itemClick:(UITapGestureRecognizer*)recognizer {
    [self itemClick:recognizer init:NO];
}

- (void)itemClick:(UITapGestureRecognizer*)recognizer init:(BOOL)init {
    NSInteger currentIndex = recognizer.view.tag;
    if(currentIndex==self.selectedIndex && !init) {
        return;
    }
    self.selectedIndex = recognizer.view.tag;
    for(int i=0;i<self.contentContainer.subviews.count; i++) {
        if(i==self.selectedIndex) {
            [self.contentContainer.subviews[i] applyStyle:@"font-size:16px; font-weight: bold; color: #EF2020;"];
        } else {
            [self.contentContainer.subviews[i] applyStyle:@"font-size:14px; font-weight: normal; color: #666666;"];
        }
        [self.contentContainer.subviews[i].yoga markDirty];
    }
    [[self getRootController] viewWillLayoutSubviews];
    // 获取选中的 item 的位置
    UILabel *selectedItem = self.contentContainer.subviews[self.selectedIndex];
    CGFloat indicatorX = selectedItem.frame.origin.x + (selectedItem.bounds.size.width - self.indictorView.bounds.size.width) / 2;
    [UIView animateWithDuration:0.3 animations:^{
        self.indictorView.yoga.left = YGPointValue(indicatorX);
        [self.indictorView.superview.yoga applyLayoutPreservingOrigin:YES];
    }];
    [self.contentContainer.yoga applyLayoutPreservingOrigin:YES dimensionFlexibility:YGDimensionFlexibilityFlexibleWidth];
    self.scrollView.contentSize =  self.contentContainer.bounds.size;
    if([self.delegate respondsToSelector:@selector(didSelectIndex:)]) {
        [self.delegate didSelectIndex:recognizer.view.tag];
    }
}

@end
